﻿using Lights.Controls;
using Lights.Types;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Lights.Page
{
    /// <summary>
    /// Logica di interazione per LibraryPage.xaml
    /// </summary>
    public partial class LibraryPage : UserControl
    {
        public event EventHandler PreExecution;
        public event ProgressChangedEventHandler ProgressChanged;
        public event RunWorkerCompletedEventHandler PostExecution;

        public IEnumerable AllPhotos { get { return Global.PhotoCollector; } }
        public IEnumerable Collections { get { return Global.CollectionCollector; } }
        public IEnumerable SelectedItems { get { return this.viewer.SelectedItems; } }

        BackgroundWorker worker;

        public void CancelAsync()
        {
            this.worker.CancelAsync();
        }

        public LibraryPage()
        {
            DataContext = this;
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.worker = new BackgroundWorker();
            this.worker.WorkerReportsProgress = true;
            this.worker.WorkerSupportsCancellation = true;
            this.worker.DoWork += worker_DoWork;
            this.worker.ProgressChanged += worker_ProgressChanged;
            this.worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Global.PhotoCollector.AddRange(e.Result as Photo[]);
            if (PostExecution != null)
                PostExecution(this, e);
        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (ProgressChanged != null)
                ProgressChanged(this, e);
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] images = e.Argument as string[];

            Photo[] photos = new Photo[images.Length];
            int i = 0;
            for (i = 0; i < images.Length && !worker.CancellationPending; i++)
            {
                photos[i] = Photo.Import(images[i]);
                worker.ReportProgress((int)((double)i / (double)images.Length * 100.0), null);
            }

            if (i < images.Length)
            {
                for (int j = 0; j < i; j++)
                {
                    File.Delete(Global.CatalogFolder + "/" + photos[j].Hashcode.ToString());
                }
                return;
            }

            e.Result = photos;
        }


        private void cmdImport_Click(object sender, RoutedEventArgs e)
        {
            frmImporter frm = new frmImporter();
            if (frm.ShowDialog().GetValueOrDefault(false))
            {
                if (PreExecution != null)
                    PreExecution(this, null);
                worker.RunWorkerAsync(frm.SelectedItems);
            }
        }
    }
}
